package com.yikes.srb.controller.web;

import com.alibaba.fastjson2.JSONObject;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.constant.hfb.RequestHelper;
import com.yikes.srb.leadingModle.form.userBind.UserBindAddForm;
import com.yikes.srb.leadingModle.form.userBind.UserBindEditForm;
import com.yikes.srb.leadingModle.form.userBind.UserBindForm;
import com.yikes.srb.leadingModle.form.userBind.UserBindQuery;
import com.yikes.srb.leadingModle.vo.UserBindVO;
import com.yikes.srb.service.UserBindService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@RequiredArgsConstructor
@Tag(name = "UserBindController", description = "用户绑定表")
@RestController
@RequestMapping("/admin/user_bind")
public class UserBindController {

    private final UserBindService service;

    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<UserBindVO>> listPage(UserBindQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<UserBindVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody UserBindAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody UserBindEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "账户绑定提交数据")
    @PostMapping("/auth/bind")
    public Result bind(@RequestBody UserBindForm form, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = service.commitBindUser(form, userId);
        return Result.ok(formStr);
    }

    @Operation(description = "账户绑定异步回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户账号绑定异步回调：" + JSONObject.toJSONString(paramMap));

        //校验签名
        if(!RequestHelper.isSignEquals(paramMap)) {
            log.error("用户账号绑定异步回调签名错误：" + JSONObject.toJSONString(paramMap));
            return "fail";
        }

        //修改绑定状态
        service.notify(paramMap);
        return "success";
    }


}
